﻿
using Microsoft.AspNetCore.Components.Forms;
using System.Text.Json;
using System.Text.RegularExpressions;

public partial class JsonHelper
{
    private const string 单行注释 = @"(?<!:)\/\/.*\n";
    private const string 多行注释 = @"\/\*(.|\n)*?\*\/";
    private const string 注释 = $"({单行注释}|{多行注释})";

    public static T Parse<T>(string filepath)
    {
        var reg = JsonAnnotate();
        string input = File.ReadAllText(filepath);
        var json = reg.Replace(input, "");
        return JsonSerializer.Deserialize<T>(json);
    }

    public static async Task<T> ParseAsync<T>(string filepath)
    {
        var reg = JsonAnnotate();
        string input = await File.ReadAllTextAsync(filepath);
        var json = reg.Replace(input, "");
        return JsonSerializer.Deserialize<T>(json);
    }


    [GeneratedRegex(注释, RegexOptions.Compiled)]
    private static partial Regex JsonAnnotate();
}